$(function() {
	{if $Think.get.order_start||$Think.get.order_end||$Think.get.year}
	$('#showmore').css('display','block')
	{/if}
	var show = 0
	var navulwid = 0;
	
	console.log($('#scrollmain').width())
	for(var wid = 0; wid < $(".amnav>ul li").length; wid++) {
		navulwid += $(".amnav>ul li").eq(wid).width()
	}
	console.log($('#scrollmain').width() < navulwid)
	if($('#scrollmain').width() < navulwid) {

		$(".amnav>ul").width(navulwid+100);
		var orderScrollnav = new IScroll('.amnav', {
			mouseWheel: true,
			scrollX: true,
			click: true
		});
	}
	$('.firstcheck').on('click',function(){
	var that=this
	var title=''
	if($(that).data('checklevel')==3){}else{
		if($(that).data('checklevel')==1){
			title='请选择复审人员'
		}else if($(that).data('checklevel')==2){
			title='请选择终审人员'
		}
	var choose=layer.open({
		  type: 1,
		  title:title,
		  skin: 'layui-layer-rim', //加上边框
		  area: ['300px', '240px'], //宽高
		  content: `
		  	<div class="am-u-sm-12" style='padding:10px'>
		  		<input style='width:100%;height:30px' type="text"/>
		  	</div>
		  	<div class="am-u-sm-12" style='padding:10px'>
		  		<button class='am-btn am-btn-sm am-btn-primary am-radius chooseappover'>确定</button>
		  		<button class='am-btn am-btn-sm am-btn-primary am-radius chooseno'>取消</button>
		  	</div>`
		  ,
		  success:function(){
		  	$('.chooseappover').on('click',function(){
		  		if($(that).data('checklevel')==1){
					$(that).html('复审')
					$(that).data('checklevel',2)
				}else if($(that).data('checklevel')==2){
					$(that).html('终审')
					$(that).data('checklevel',3)
				}
				layer.close(choose)
		  	})
		  	$('.chooseno').on('click',function(){
		  		layer.close(choose)
		  	})
		  	
		  }
		});
	}
		
	})
//	$('.changedriver').on('click',function(){
//		var order_id=$(this).closest('tr').data('id')
//		layer.open({
//		    type: 1,
//		    title:'选择司机',
//		    skin: 'layui-layer-rim', //加上边框
//		    area: ['300px', '240px'], //宽高
//		    content: `<div class="am-u-sm-12">
//		    		<form class="am-form tpl-form-border-form" data-check="1" data-ajax="1">
//		    		<div class='am-u-sm-12' style='margin-bottom:10px'>
//		  				<input type="hidden" class="driver_id" name="driver_id"/>
//						<input type="text" class="am-form-field tpl-form-no-bg driver" style="line-height: 21px;" data-department="1" data-check='require' data-msg="请选择司机"  placeholder="请选择司机" readonly="" >
//		  			</div>
//		  			<div class='am-u-sm-12' style='margin-bottom:10px'>
//		  				<input type="hidden" class="CarId" name="car_id" value="{$item.car_id}" />
//						<input type="text" class="am-form-field tpl-form-no-bg  car_number" value="{$item.number}" placeholder="请选择车辆"  readonly=""/>
//		  			</div>
//		  			<div class='am-u-sm-12'>
//		  				<button type='button' class='am-btn am-btn-sm am-btn-primary am-radius change_sub'>提交</button>
//		  			</div>
//		  			</form>
//		  			</div>`,
//		  	success:function(){
//		  		$('.driver').on('click',function(){
//					choosecar("{:url('admin/driver/index',['op'=>'search'])}")
//				});
//				$('.car_number').on('click',function(){
//					choosecar("{:url('car/index',['op'=>'search'])}")
//				});
//				$('.change_sub').on('click',function(){
//					var driver_id=$('.driver_id').val()
//					console.log(driver_id)
//					console.log(order_id)
//					Load()
//					$.ajax({
//						type:"post",
//						url:"",
//						data:{
//							driver_id:driver_id,
//							order_id:order_id
//						},
//						success:function(res){
//							Load(1)
//							window.location.reload()
//							if(res.code==1){
//								window.location.reload()
//							}
//						},
//						error:function(res){
//							Load(1)
//							Err('请求失败')
//						}
//					});
//				});
//		  	}
//		});
//	})

	//免单 BY 发仔 2018-08-27
	$('.setFree').click(function () {
		let tr = $(this).closest('tr');
		let oid = tr.data('id');
		layer.confirm('是否确认免单？',function () {
            // alert(oid);
			$.post("{:url('setFree')}",{oid:oid},function (res) {
				if(res.code) {
				    Msg('成功免单');
				    tr.remove();
				} else {
				    Err('免单失败，请检查该订单是否满足免单条件');
                }
            });
        }
        );
    });

	//管理员代支付 2018-08-27 BY 发仔
	$('.adminOrderPay').click(function () {
        let tr = $(this).closest('tr');
        let oid = tr.data('id');
        layer.confirm('是否确认支付？',function () {
                // alert(oid);
                $.post("{:url('adminOrderPay')}",{oid:oid},function (res) {
                    if(res.code) {
                        Msg('成功支付');
                        tr.remove();
                    } else {
                        Err('支付失败，请检查该订单是否满足支付条件');
                    }
                });
            }
        );
    });

//	if($('#scrollmain').width() < $('.am-tabs-bd table').width()) {
//		$(".am-tabs-bd>div").width($(".am-tabs-bd>div table").width());
//		var orderScrollmain = new IScroll('.am-tabs-bd', {
//			mouseWheel: true,
//			scrollX: true,
//			click: true,
//			scrollbars: true
//		});
//	}
	$('.check').on('click',function(){
		var that=this
		var auto = layer.open({
   		type: 1,
   		title:false,
   		skin: 'layui-layer-rim', //加上边框
   		area: ['200px', '100px'], //宽高
   		content: `
   			<div class='am-u-sm-12' style='padding:10px'>
   				<div class='am-u-sm-12' style='text-align:center;margin-bottom:10px'>是否确认该订单</div>
   				<div class='am-u-sm-12' style='text-align:center'>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_ok'>审批</a>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_no'>取消</a>
   				</div>
   			</div>
   		`,
   		success:function(){
   			
   			$('.close_ok').on('click',function(){
   				var id=$(that).closest('tr').data('id')
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						order_id:id,
   						order_state:2
   					},
   					url:'{:url("edit")}',
   					success:function(res){
   						Load(1)
   						if(res.code==1){
	   						Msg('审批成功');
	   						window.history.go(0)
   						}else{
   							Err(res.msg)
   						}
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
   				layer.close(auto)
   			});
   			$('.close_no').on('click',function(){
   				layer.close(auto)
   			})
   		}
   		})
	})

	$('.company_name').on('click', function() {
		choosecar("{:url('company/index',['op'=>'search'])}")
	});
	//	$('.orderertype').on('change', function() {
	//		if($('.orderertype').val() == 1) {
	//			console.log(1)
	//			$('.passenger').attr('name', 'passenger')
	//			$('.passenger').attr('type', 'text')
	//			$('.orderer').removeAttr('name')
	//			$('.orderer').attr('type', 'hidden')
	//		} else {
	//			console.log(2)
	//			$('.passenger').attr('type', 'hidden')
	//			$('.passenger').removeAttr('name')
	//			$('.orderer').attr('name', 'orderer')
	//			$('.orderer').attr('type', 'text')
	//
	//		}
	//	})
//	$('.dispatch').on('click',function(){
//		layer.open({
//				type: 1,
//				title: false,
//				id: "dispatch" //防止重复弹出
//					,
//				content: `<div>
//					<input type="hidden" class="car_use_id" name="car_number" value="{$item.car_use_id}" />
//					<input type="text" name='number' class="am-form-field tpl-form-no-bg car_number" style="line-height: 21px;" data-department="1" data-check='require' data-msg="公司/单位不能为空" value="{$item.car_use_name}" placeholder="点击选择公司/单位" readonly="" >
//				</div>`,
//				btn: ['确定', '取消'],
//				btnAlign: 'c' //按钮居中
//					,
//				shade: 0 //不显示遮罩
//					,
//				yes: function() {
//					
//
//				},
//				btn2: function() {
//					layer.closeAll();
//				}
//			});
//	})
	$("#more").on("click", function() {
		if(show == 0) {

			$('#showmore').css('display', 'block')
			//			$('.arrive_time').attr('name', 'arrive_time')
			//			$('.complete_time').attr('name', 'complete_time')
			//			$('.apply_time').attr('name', 'apply_time')
			//			$('.approval_time').attr('name', 'approval_time')
			show = 1
		} else {
			$('#showmore').css('display', 'none')
			show = 0
			//			$('.arrive_time').removeAttr('name')
			//			$('.complete_time').removeAttr('name')
			//			$('.apply_time').removeAttr('name')
			//			$('.approval_time').removeAttr('name')
		}
	})
	$(".showmap").on('click', function() {
		popup(this)
		var that = this
		if($(this).data('method') == "showmap") {
			var trackload = layer.load(1, {
				shade: [0.1, '#fff'] //0.1透明度的白色背景
			});
			var map = new AMap.Map('showlocation', {
				resizeEnable: true,
				zoom: 8,
				center: [118.57, 24.82]
			});
			AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) {
				var pathSimplifierIns = new PathSimplifier({
					zIndex: 100,
					map: map, //所属的地图实例
					getPath: function(pathData, pathIndex) {
						//返回轨迹数据中的节点坐标信息，[AMap.LngLat, AMap.LngLat...] 或者 [[lng|number,lat|number],...]
						return pathData.path;
					},
					getHoverTitle: function(pathData, pathIndex, pointIndex) {
						//返回鼠标悬停时显示的信息
						if(pointIndex >= 0) {
							//鼠标悬停在某个轨迹节点上
							return pathData.name + '，点:' + pointIndex + '/' + pathData.path.length;
						}
						//鼠标悬停在节点之间的连线上
						return pathData.name + '，点数量' + pathData.path.length;
					},
					renderOptions: {
						//轨迹线的样式
						pathLineStyle: {
							strokeStyle: 'red',
							lineWidth: 6,
							dirArrowStyle: true
						}
					}
				});
				$.ajax({
					type: "post",
					url: "{:url('getTrack')}",
					data: {
						oid: $(that).closest('tr').data('id')
					},
					success: function(res) {
						layer.close(trackload);
						var allpath = []
						var speed=0;
						if(typeof res == 'string') {
							var opath1 = JSON.parse(res).data;
						} else {
							var opath1 = res.data;
						}
						var opath2=[];
						var opath=[];
						//根据时间进行排序
						if(opath1.length>1){
							for(var rankidx=0;rankidx<opath1.length;rankidx++){
								opath2.push(opath1[rankidx].time)
							}
							opath2.sort()
							for(var k=0;k<opath2.length;k++){
								for(var j=0;j<opath1.length;j++){
									if(opath2[k]==opath1[j].time){
										opath.push(opath1[j])
									}
								}
							}
						console.log(opath)
						
						for(var oi = 0; oi < opath.length; oi++) {
							var oarr = []
							oarr.push(opath[oi].longitude)
							oarr.push(opath[oi].latitude)
							allpath.push(oarr)
						}
						initPage(PathSimplifier, pathSimplifierIns, map, allpath)
						}else{
							Err('暂无轨迹信息')
						}
					},
					error:function(){
						layer.close(carload);
						Err('请求失败')
					}
				});
			})
		}
	});
	$('.car_type').on('click', function() {
		choosecar("{:url('models/index',['op'=>'search'])}")
	});
	$('.cancel').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.consent').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.repulse').on('click', function() {
		popup(this, "{:url('state')}");
	});
	$('.am-nav-tabs li a').on('click', function() {
		var index = layer.load(1, {
			shade: [0.1, '#fff'] //0.1透明度的白色背景
		});
	});
	$('.order_start').on('change',function(){
		datejudge($('.order_start'), $('.order_end'))
	})
	$('.order_end').on('change',function(){
		datejudge($('.order_start'), $('.order_end'))
	})
	function initPage(PathSimplifier, pathSimplifierIns, map, path) {
		var alljourney=0
		for(var pidx=0;pidx<path.length-1;pidx++){
			var  journey=Math.sqrt(Math.pow((path[pidx][0]-path[pidx+1][0]),2)+Math.pow((path[pidx][1]-path[pidx+1][1]),2))*111000
			alljourney+=journey
		}
		
		console.log(alljourney)
		//这里构建两条简单的轨迹，仅作示例
		pathSimplifierIns.setData([{
			name: '轨迹',
			path: path
		}]);
		
		//创建一个巡航器
		var navg0 = pathSimplifierIns.createPathNavigator(0, //关联第1条轨迹
			{
				loop: true, //循环播放
				speed:alljourney/10,
				pathNavigatorStyle: {
					width: 16,
					height: 32,
					content: PathSimplifier.Render.Canvas.getImageContent('{$static_dir}/assets/img/car-gray.png', onload, onerror),
					strokeStyle: null,
					fillStyle: null
				}
			});

		navg0.start();
	}

})